package config

import (
	"log"
	"os"

	"gopkg.in/yaml.v2"
)

type Config struct {
	Mysql struct {
		DataSource string `yaml:"dataSource"`
	} `yaml:"mysql"`
	RPCAddr    string `yaml:"rpcAddr"`
	RPCTimeout int    `yaml:"rpcTimeout"`
	Redis      struct {
		Addr     string `yaml:"addr"`
		Password string `yaml:"password"`
		DB       int    `yaml:"db"`
	} `yaml:"redis"`
	Server struct {
		RunMode string `yaml:"runMode"`
		Port    string `yaml:"port"`
	} `yaml:"server"`
}

func InitConfig() *Config {
	yamlFile, err := os.ReadFile("config/env.yaml")
	if err != nil {
		log.Fatalf("yamlFile.Get err   #%v ", err)
	}
	var config Config
	err = yaml.Unmarshal(yamlFile, &config)
	if err != nil {
		log.Fatalf("Unmarshal: %v", err)
	}
	return &config
}

func (c *Config) GetMysqlDataSource() string {
	return c.Mysql.DataSource
}
